package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.MonthWeekly;
import com.neusoft.neuiotms.dcom.model.QMonthWeekly;
import com.neusoft.neuiotms.dcom.repository.CustomizedMonthWeeklyRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedMonthWeeklyRepositoryImpl extends AbstractRepository implements CustomizedMonthWeeklyRepository {

    @Override
    public List<MonthWeekly> index(final Searchable searchable) {
        final QMonthWeekly qMonthWeekly = QMonthWeekly.monthWeekly;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<MonthWeekly> query = this.query().selectFrom(qMonthWeekly);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<MonthWeekly> search(final Pageable pageable, final Searchable searchable) {
        final QMonthWeekly qMonthWeekly = QMonthWeekly.monthWeekly;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<MonthWeekly> query = this.query().selectFrom(qMonthWeekly);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return MonthWeekly.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QMonthWeekly qMonthWeekly = QMonthWeekly.monthWeekly;

        if (!StringUtils.isEmpty(searchable.getStrValue("clientCode"))) {
            where.and(super.equalsStr(qMonthWeekly.clientCode, searchable, "clientCode"));
        }
        // 客户名称
        if (!StringUtils.isEmpty(searchable.getStrValue("clientName"))) {
            where.and(super.containsStr(qMonthWeekly.clientName, searchable, "clientName"));
        }

        if (null != searchable.getStrArray("institutionCodeArray")) {
            where.and(super.inStrings(qMonthWeekly.clientCode, searchable, "institutionCodeArray"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("residentEngineerUserCode"))) {
            where.and(super.equalsStr(qMonthWeekly.residentEngineerUserCode, searchable, "residentEngineerUserCode"));
        }

        if (null != searchable.getLongValue("operationTime")) {
            final long monthStartUnix = searchable.getLongValue("operationTime");
            final Date startMonth = DateUtils.round(new Date(monthStartUnix), Calendar.MONTH);
            final Date endMonth = DateUtils.ceiling(new Date(monthStartUnix), Calendar.MONTH);
            where.and(qMonthWeekly.operationTime.before(endMonth));
            where.and(qMonthWeekly.operationTime.goe(startMonth));
        }

        if (null != searchable.getLongValue("afEqReportDate")) {
            where.and(super.afterOrEqualsDate(qMonthWeekly.operationTime, searchable, "afEqReportDate"));
        }

        if (null != searchable.getLongValue("beEqReportDate")) {
            where.and(super.beforeOrEqualsDate(qMonthWeekly.operationTime, searchable, "beEqReportDate"));
        }

        return where;
    }
}
